package util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * DataLogger
 * Logs Data and saves it in a file
 * 
 * @author Jan Quenzel
 * @author Rico Flaegel
 */
public class DataLogger{
	/**
	 * Location where the file will be saved
	 */
	private String location;
	/**
	 * current File
	 */
	private File f;
	/**
	 * writer to the current File
	 */
	private BufferedWriter writer;
	/**
	 * Counter if more than one Logger will be used by the calling instance
	 */
	private static AtomicInteger cnter = new AtomicInteger(0);
	
	public DataLogger(String location) throws IOException{
		this.location = cnter.getAndIncrement()+"_"+location;
		f = new File(this.location);
		if(f.exists()){
			f.delete();
		}
		f.createNewFile();
		writer = new BufferedWriter(new FileWriter(f));
	}
	/**
	 * Logs a string to the current File.
	 * @param logThis String which should be logged
	 */
	public synchronized void logString(String logThis){
		try {
			writer.write(System.currentTimeMillis() + " - " + logThis + "\n");
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}